import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Demo1 {
    public static void main(String[] args) {
        int[] nums = {0,1,2,3,4,0,0,7,8,9,10,11,12,0,1};
        System.out.println(containsNearbyDuplicate(nums, 2));
    }
    public static boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if(map.containsKey(nums[i])){
                if(Math.abs(map.get(nums[i]) - i ) <= k){
                    return true;
                }
            }
            map.put(nums[i],i);
        }
        return false;
    }
}
